#include "sms.h"

#define PROMPTBAR_LAYOUT_NAME "layout-promptbar"

typedef struct _Promptbar {
	gdouble hide_x;
	gdouble hide_y;
	gdouble show_x;
	gdouble show_y;
	gdouble width;
	gdouble height;

	GooCanvasItem* group;
	GooCanvasItem *bg;
}Promptbar;


static gboolean promptbar_create_bg(xmlNode *a_node, Layout *layout)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar=layout->private;
	SMS *sms=layout->sms;
	GdkPixbuf *pixbuf;
	char *val,buf[256];

	if(!a_node)
		return FALSE;

	promptbar->hide_x=(gdouble)sms_get_xml_double(a_node,"hide_x",0);
	promptbar->hide_y=(gdouble)sms_get_xml_double(a_node,"hide_y",0);
	promptbar->show_x=(gdouble)sms_get_xml_double(a_node,"show_x",0);
	promptbar->show_y=(gdouble)sms_get_xml_double(a_node,"show_y",0);
	promptbar->width=(gdouble)sms_get_xml_double(a_node,"w",0);
	promptbar->height=(gdouble)sms_get_xml_double(a_node,"h",0);

	if(promptbar->width==0||promptbar->height==0){
		fprintf(stderr,"%s:Taskbar get size error.w=%f,h=%f\n",
				__FUNCTION__, promptbar->width, promptbar->height);
		return FALSE;
	}

	val = xmlGetProp (a_node, (xmlChar*)"pic");
	if(!val)
		return FALSE;

	g_object_set(layout->cavas, "x", promptbar->show_x,
			                      "y", promptbar->show_y,
			                      "width", promptbar->width,
			                      "height", promptbar->height, NULL);

	sprintf(buf, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, sms->theme->name, layout->name, val);
	pixbuf = sms_load_pic(buf, promptbar->width, promptbar->height);
	promptbar->bg = goo_canvas_image_new(layout->cavas, pixbuf, 0, 0, NULL);
	//g_object_set(promptbar->bg, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
	goo_canvas_item_translate(promptbar->bg, promptbar->hide_x - promptbar->show_x, promptbar->hide_y - promptbar->show_y);
	
	//goo_canvas_item_set_simple_transform(promptbar->bg, promptbar->show_x, promptbar->show_y, 1, 0);

	//free memory
	g_object_unref(pixbuf);
	return TRUE;
}

static gboolean promptbar_travel_xml(xmlNode *a_node,Layout *layout)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar=layout->private;
	char *val;
	xmlNode *cur_node = NULL;
	int icons=0;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name, "prompt-bar")==0){
				return promptbar_create_bg(cur_node, layout);
				//cur_node=cur_node->children;
				//continue;
			}/*else if(strcmp(cur_node->name,"icon")==0){
				if(taskbar_ticon_create(cur_node,layout))
					icons++;
			}*/
		}
	}


	return FALSE;
}

static gboolean promptbar_load_config(Layout *layout)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar=layout->private;
	SMS *sms=layout->sms;

	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;

	char conf[256];

	sprintf(conf, "%s/%s/layout/%s/layout.xml", SMS_RESOURCE_DIR, "default", layout->name);
	printf("conf: %s\n", conf);
	doc = xmlReadFile(conf, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf);
		return ret;
	}

	root_element = xmlDocGetRootElement(doc);

	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
			ret=promptbar_travel_xml(cur_node, layout);
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();
	return ret;
}


static gboolean promptbar_init(Layout *layout, gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar=(Promptbar *)g_malloc0(sizeof(Promptbar)+1);

	layout->private=(void *)promptbar;

	layout->cavas = goo_canvas_group_new(layout->parent, NULL);	

	if(!promptbar_load_config(layout))
	{
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		return FALSE;//exit(-1);
	}
	return TRUE;
}

static gboolean promptbar_clear (Layout *layout, gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar = layout->private;

	if (promptbar->bg)
		goo_canvas_item_remove(promptbar->bg);
	if (promptbar->group)
		goo_canvas_item_remove(promptbar->group);
	g_free(promptbar);

	return TRUE;
}

static gboolean promptbar_show(Layout * layout, gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar = layout->private;
	goo_canvas_item_animate (promptbar->bg, 0, 0, 
			                                1, 0, TRUE, 800, 40, GOO_CANVAS_ANIMATE_FREEZE);
	return TRUE;
}

static gboolean promptbar_hide(Layout * layout, gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Promptbar *promptbar = layout->private;
	goo_canvas_item_animate (promptbar->bg, promptbar->hide_x - promptbar->show_x, promptbar->hide_y - promptbar->show_y, 
				                            1, 0, TRUE, 800, 40, GOO_CANVAS_ANIMATE_FREEZE);
	return TRUE;
}

static gboolean promptbar_key_press(Layout * layout, GdkEventKey *event)
{
	printf("start func %s\n" ,__FUNCTION__);
	/*Promptbar *promptbar = layout->private;
	if(event->keyval==GDK_A){
		if(promptbar->isShow){
			return  promptbar_hide(layout, NULL);
		} else {
			return promptbar_show(layout, NULL);
		}
	}*/
	return FALSE;
}

Layout *promptbar_layout_new(GooCanvasItem * parent, gpointer sms)
{
	Layout *layout=layout_new(parent, LAYOUT_TYPE_ITEM, LAYOUT_ITEM_PROMPTBAR, PROMPTBAR_LAYOUT_NAME, sms);

	layout_set_func(layout, "init", promptbar_init);
	layout_set_func(layout, "clear", promptbar_clear);
	layout_set_func(layout, "key-press", promptbar_key_press);
	layout_set_func(layout, "show", promptbar_show);
	layout_set_func(layout, "hide", promptbar_hide);
	return layout;
}
